import { propsToAttrMap } from '@vue/shared';
import { FormSchema } from '/@/components/Table';
import { useMessage } from '/@/hooks/web/useMessage';
const { createMessage } = useMessage();

export const formSchema: FormSchema[] = [
  {
    field: 'btnId',
    label: 'ID',
    required: true,
    component: 'Input',
    colProps: { span: 24 },
  },
  {
    field: 'linkName',
    label: '名称',
    required: true,
    component: 'Input',
    colProps: { span: 24 },
  },
  {
    field: 'title',
    label: '提示',
    component: 'Input',
    colProps: { span: 24 },
  },
  {
    field: 'enabled',
    label: '启用',
    component: 'Checkbox',
    defaultValue: true,
    colProps: { span: 24 },
  },
  {
    field: 'filter',
    label: '条件',
    component: 'Input',
    colProps: { span: 24 },
    helpMessage: '当满足条件时，显示链接',
    slot: 'filter',
  },
  {
    field: 'event',
    label: '事件',
    component: 'Input',
    colProps: { span: 24 },
    helpMessage: '(注：点击事件方法中如有双引号将会被自动替换为单引号)',
    slot: 'event',
    ifShow: ({ values }) => {
      return values.btnId !== 'btnPrint' && values.btnId !== 'btnEdit';
    },
  },
  {
    field: 'role',
    label: '角色',
    component: 'Input',
    colProps: { span: 24 },
    slot: 'role',
  },
];

export const linkEventOptions = [
  {
    label: '链接',
    value: 'link',
  },
  {
    label: '点击',
    value: 'click',
  },
  {
    label: '发起流程',
    value: 'flow',
  },
  {
    label: '进入模块',
    value: 'module',
  },
  {
    label: '添加模块',
    value: 'moduleAdd',
  },
];

export const modeList = [
  {
    label: '对话框',
    value: 1,
  },
  // {
  //   label: '抽屉',
  //   value: 0,
  // },
];

export function loadProp(prop) {
  const record = {
    linkName: prop.name || '',
    linkHref: prop.href || '',
    linkCond: prop.cond || '',
    linkEvent: prop.event || '',
    roleCodes: prop.role || '',
    btnId: prop.id || '',
    title: prop.title || '',
    flowTypeCode: '',
    params: '',
    enabled: prop.enabled || false,
    moduleCode: prop.moduleCode,
    mode: 1,
  };
  if (prop.event == 'flow') {
    console.log('prop.href', prop.href);
    const json = $.parseJSON(decodeJSON(prop.href));
    console.log('json', json);
    record.flowTypeCode = json.flowTypeCode;
    if (json.params) {
      record.params = JSON.parse(json.params);
    }
  } else if (prop.event == 'module') {
    console.log('decodeJSON(prop.href)', decodeJSON(prop.href));
    const json = $.parseJSON(decodeJSON(prop.href));
    record.moduleCode = json.moduleCode;
    if (json.params) {
      record.params = JSON.parse(json.params);
    }
  } else if (prop.event == 'moduleAdd') {
    const json = $.parseJSON(decodeJSON(prop.href));
    record.moduleCode = json.moduleCode;
    record.mode = json.hasOwnProperty('mode') ? json.mode : 1;
  } else if (prop.event == 'click') {
    record.linkHref = decodeJSON(prop.href);
  }
  return record;
}
export function makeBtnJson(prop, position) {
  const message = {
    isAlow: false,
    record: {
      title: '',
      event: '',
      href: '',
      role: '',
      params: '',
      name: '',
      id: '',
      enabled: false,
      cond: '',
      moduleCode: '',
      mode: '',
    },
  };
  if (!prop.btnId && position != 'form') {
    createMessage.warning('请填写ID');
    message.isAlow = true;
    return message;
  }
  if (!prop.linkName && position != 'form') {
    createMessage.warning('请填写名称');
    message.isAlow = true;
    return message;
  }

  let linkHref = prop.linkHref;
  const linkName = prop.linkName;
  const linkCond = prop.linkCond;
  const linkEvent = prop.linkEvent;
  const linkRole = prop.roleCodes;
  let flowTypeCode = '',
    moduleCode = '';
  if (linkEvent == 'flow') {
    flowTypeCode = prop.flowTypeCode;
    if (!flowTypeCode || 'not' == flowTypeCode) {
      createMessage.warning('请选择流程！');
      message.isAlow = true;
      return message;
    }
    const json = {
      flowTypeCode: '',
      params: '',
    };

    json.flowTypeCode = flowTypeCode;
    if (prop.params) {
      json.params = JSON.stringify(prop.params);
    }

    linkHref = encodeJSON(JSON.stringify(json));
  } else if (linkEvent == 'module') {
    moduleCode = prop.moduleCode;
    if (!moduleCode || 'not' == moduleCode) {
      createMessage.warning('请选择模块！');
      message.isAlow = true;
      return message;
    }
    const json = {
      moduleCode: '',
      params: '',
    };

    json.moduleCode = moduleCode;
    if (prop.params) {
      json.params = JSON.stringify(prop.params);
    }

    linkHref = encodeJSON(JSON.stringify(json));
  } else if (linkEvent == 'moduleAdd') {
    moduleCode = prop.moduleCode;
    if (!moduleCode || 'not' == moduleCode) {
      createMessage.warning('请选择模块！');
      message.isAlow = true;
      return message;
    }
    const json = {
      moduleCode: '',
      mode: '',
    };

    json.moduleCode = moduleCode;
    json.mode = prop.mode;

    linkHref = encodeJSON(JSON.stringify(json));
  } else {
    // 替换操作列链接点击事件中的双引号为单引号，以免生成如href="javascript:fun(arg1, "arg2")"，致出错
    linkHref = linkHref ? linkHref.replaceAll('"', "'") : '';
    linkHref = encodeJSON(linkHref); // 替换掉javascript方法中用以间隔参数的逗号
  }

  message.record.href = linkHref;
  message.record.name = linkName;
  message.record.cond = linkCond;
  message.record.event = linkEvent;
  message.record.role = linkRole;
  message.record.id = prop.btnId;
  message.record.title = prop.title;
  message.record.enabled = prop.enabled;
  if (position == 'form') {
    message.record.enabled = true;
  }
  return message;
}
function decodeJSON(jsonString) {
  jsonString = jsonString.replace(/%dq/gi, '"');
  jsonString = jsonString.replace(/%sq/gi, "'");
  return jsonString;
}

// 对字符串中的引号进行编码，以免引起json解析问题
function encodeJSON(jsonString) {
  jsonString = jsonString.replace(/\"/gi, '%dq');
  return jsonString.replace(/'/gi, '%sq');
}
